package cn.ekuma.epos.datalogic.define.dao.erp;

import java.util.List;

import com.openbravo.bean.erp.viewbean.CustomerCategoryTransaction;
import com.openbravo.bean.erp.viewbean.CustomerTransaction;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.QBFBuilder;
import com.openbravo.data.loader.SerializerRead;
import com.openbravo.data.loader.SerializerWriteBasic;
import com.openbravo.data.loader.Session;
import com.openbravo.data.loader.StaticSentence;
import com.openbravo.data.loader.query.QBFParameters;

import cn.ekuma.data.dao.ViewDAO;
import cn.ekuma.epos.datalogic.define.dao.erp.CustomerCategoryTransactionDAO.PosTransactionRead;

public class CustomerTransactionDAO extends ViewDAO<CustomerTransaction> {

	public CustomerTransactionDAO(I_Session s) {
		super(s);
	}

	@Override
	public CustomerTransaction readValues(DataRead dr,CustomerTransaction obj) throws BasicException {
		if(obj==null)
			obj=new CustomerTransaction();
		obj.setCustomerId(dr.getString(1));
		obj.setOrderTypeName(dr.getString(2));
		obj.setOrderType(dr.getInt(3));
		obj.setTotal(dr.getDouble(4));
		obj.setDebtTotal(dr.getDouble(5));
		return obj;
	}

	@Override
	public Class getSuportClass() {
		return CustomerTransaction.class;
	}
	
	/**public List<CustomerTransaction> customerCategoryTransactionListQBF(Object filter) throws BasicException{
        return new StaticSentence(s, new QBFBuilder(
                "SELECT  PS.ID,E.NAME, T.ORDERTYPE, SUM(T.TOTALPRICE), SUM(T.DEBT)  "
                + "FROM  ORDERS T "
                + "LEFT  JOIN ORDERTYPES E ON T.DEFINETYPEREF = E.ID "
                + "LEFT JOIN CUSTOMERS PS ON T.CUSTOMER = PS.ID "
                + "LEFT OUTER JOIN OPERATION_CUSTOMER OP ON T.CUSTOMER = OP.CUSTOMER  "
                + "LEFT OUTER JOIN OPERATION_CUSTOMER_CATEGORIES OPC ON OP.OPERATIONCATEGORIE = OPC.ID  "
                + "WHERE T.CUSTOMER IS NOT NULL AND ?(QBF_FILTER)  "
                + "GROUP BY PS.ID , E.NAME, T.ORDERTYPE  ",
                new String[]{"T.CURDATE", "T.CURDATE","OPC.ID"}), new SerializerWriteBasic(new Datas[]{Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.STRING}), this).list(filter);
    }

    public  List<CustomerTransaction> customerCategoryPOSTransactionListQBF(Object filter) throws BasicException {
        return new StaticSentence(s, new QBFBuilder(
                "SELECT  PS.ID , T.TICKETTYPE, SUM(T.TOTALPRICE)  "
                + "FROM TICKETS T "
                + "LEFT JOIN CUSTOMERS PS ON T.CUSTOMER = PS.ID "
                + "LEFT OUTER JOIN OPERATION_CUSTOMER OP ON T.CUSTOMER = OP.CUSTOMER  "
                + "LEFT OUTER JOIN OPERATION_CUSTOMER_CATEGORIES OPC ON OP.OPERATIONCATEGORIE = OPC.ID  "
                + "WHERE T.CUSTOMER IS NOT NULL AND ?(QBF_FILTER) "
                + "GROUP BY PS.ID, T.TICKETTYPE ", 
                new String[]{"T.CURDATE", "T.CURDATE", "OPC.ID"}), new SerializerWriteBasic(new Datas[]{Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.TIMESTAMP,  Datas.OBJECT, Datas.STRING}), new PosTransactionRead()).list(filter);
    }**/
    
    public static class PosTransactionRead implements SerializerRead<CustomerTransaction>{
		@Override
		public CustomerTransaction readValues(DataRead dr) throws BasicException {
			CustomerTransaction obj=new CustomerTransaction();
			obj.setCustomerId(dr.getString(1));
			obj.setOrderTypeName("POS");
			obj.setOrderType(dr.getInt(2));
			obj.setTotal(dr.getDouble(3));
			obj.setDebtTotal(0);
			return obj;
		}
    }
    
    public List<CustomerTransaction> customerTransactionListQBF(
			QBFParameters transactionFilter)throws BasicException{
    	 return new StaticSentence(s, new QBFBuilder(
                 "SELECT  PS.ID,E.NAME, T.ORDERTYPE, SUM(T.TOTALPRICE), SUM(T.DEBT)  "
                 + "FROM  ORDERS T "
                 + "LEFT  JOIN ORDERTYPES E ON T.DEFINETYPEREF = E.ID "
                 + "LEFT JOIN CUSTOMERS PS ON T.CUSTOMER = PS.ID "
                 + "LEFT OUTER JOIN OPERATION_CUSTOMER OP ON T.CUSTOMER = OP.CUSTOMER  "
                 + "LEFT OUTER JOIN OPERATION_CUSTOMER_CATEGORIES OPC ON OP.OPERATIONCATEGORIE = OPC.ID  "
                 + "WHERE T.CUSTOMER IS NOT NULL AND ?(QBF_FILTER)  "
                 + "GROUP BY PS.ID , E.NAME, T.ORDERTYPE  "), null, this).list(transactionFilter);
	
	}

	public List<CustomerTransaction> customerPOSTransactionListQBF(
			QBFParameters transactionFilter)throws BasicException{
		return new StaticSentence(s, new QBFBuilder(
                "SELECT  PS.ID , T.TICKETTYPE, SUM(T.TOTALPRICE)  "
                + "FROM TICKETS T "
                + "LEFT JOIN CUSTOMERS PS ON T.CUSTOMER = PS.ID "
                + "LEFT OUTER JOIN OPERATION_CUSTOMER OP ON T.CUSTOMER = OP.CUSTOMER  "
                + "LEFT OUTER JOIN OPERATION_CUSTOMER_CATEGORIES OPC ON OP.OPERATIONCATEGORIE = OPC.ID  "
                + "WHERE T.CUSTOMER IS NOT NULL AND ?(QBF_FILTER) "
                + "GROUP BY PS.ID, T.TICKETTYPE "), null, new PosTransactionRead()).list(transactionFilter);
	}

}
